package edu.zhku.ttms.view.menuBar;

import edu.zhku.ttms.service.UserService;
import edu.zhku.ttms.service.impl.UserServiceImpl;
import edu.zhku.ttms.utils.LoginSession;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.panel.content.*;

import javax.swing.*;
import java.awt.event.ActionListener;

/**
 * @author Feng
 * @since 2021/4/23 21:57
 */
public class MyMenuBar extends JMenuBar {

    UserService userService = new UserServiceImpl();

    public MyMenuBar() {
        int role = LoginSession.getLoggedUserRole();
        MyMenu menu = new MyMenu("菜单");
        if (role == 1) {
            menu.addActionItem("车辆管理", e -> ViewController.changeView(new CarManagementPanel()));
            menu.addActionItem("交警管理", e -> ViewController.changeView(new TrafficPoliceManagementPanel()));
        } else if (role == 2) {
            menu.addActionItem("所开罚单", e -> ViewController.changeView(new TicketManagementPanel()));
        } else {
            menu.addActionItem("罚单缴费", e -> ViewController.changeView(new TicketPayPanel()));
        }
        menu.addActionItem("回到首页", e -> ViewController.changeView(new SuccessPanel()));
        this.add(menu);

        MyMenu userCenterMenu = new MyMenu("个人中心");
        userCenterMenu.addActionItem("修改密码", e -> ViewController.changeView(new ModPwdPanel()));
        userCenterMenu.addActionItem("注销", e -> {
            userService.logout();
            ViewController.changeView(new LoginPanel());
        });

        this.add(userCenterMenu);
    }
}

class ActionMenuItem extends JMenuItem {
    public ActionMenuItem(String title, ActionListener e) {
        super(title);
        this.addActionListener(e);
    }
}

class MyMenu extends JMenu {

    public MyMenu(String title) {
        super(title);
    }

    public void addActionItem(ActionMenuItem item) {
        this.add(item);
    }

    public void addActionItem(String title, ActionListener e) {
        this.addActionItem(new ActionMenuItem(title, e));
    }

}
